package dao;

import utils.connect.ConnectSqlDaoImpl;
import utils.string.sql.T_Equipment;
import vo.Equipment;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class EquipmentDaoImpl implements EquipmentDao {
    ConnectSqlDaoImpl connectSqlDao = ConnectSqlDaoImpl.getInStance();

    @Override
    public Equipment selectByID(int id) {
        Equipment equipment = new Equipment();
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "select * from "+ T_Equipment.TABLE_NAME+" where "
                +T_Equipment.EQUIP_ID+"= ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                equipment.setEquip_id(resultSet.getInt(T_Equipment.EQUIP_ID));
                equipment.setEquip_name(resultSet.getString(T_Equipment.EQUIP_NAME));
                equipment.setEquip_num(resultSet.getInt(T_Equipment.EQUIP_NUM));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return equipment;
    }

    @Override
    public boolean deleteByID(int id) {
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "delete from "+ T_Equipment.TABLE_NAME+" where "
                +T_Equipment.EQUIP_ID+"= ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            preparedStatement.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public HashMap selectAll() {
        HashMap equipments = new HashMap();
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "select * from "+ T_Equipment.TABLE_NAME;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                Equipment equipment = new Equipment();
                equipment.setEquip_id(resultSet.getInt(T_Equipment.EQUIP_ID));
                equipment.setEquip_name(resultSet.getString(T_Equipment.EQUIP_NAME));
                equipment.setEquip_num(resultSet.getInt(T_Equipment.EQUIP_NUM));
                equipments.put(equipment.getEquip_id(),equipment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return equipments;
    }

    @Override
    public boolean updateById(Equipment equipment, int id) {
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "update "+ T_Equipment.TABLE_NAME+" set "+T_Equipment.EQUIP_NAME +"= ? ,"
                +T_Equipment.EQUIP_NUM + " = ? where "+ T_Equipment.EQUIP_ID +" = ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,equipment.getEquip_name());
            preparedStatement.setInt(2,equipment.getEquip_num());
            preparedStatement.setInt(3,id);
            preparedStatement.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean insertEquipment(Equipment equipment) {
        try {
            connectSqlDao.initConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection connection = connectSqlDao.getConnection();
        String sql = "insert into "+ T_Equipment.TABLE_NAME+"("+T_Equipment.EQUIP_NAME+","+T_Equipment.EQUIP_NUM+") values (?,?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,equipment.getEquip_name());
            preparedStatement.setInt(2,equipment.getEquip_num());
            preparedStatement.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connectSqlDao.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean changeEquipment(int id, int num, int handle_way) {
        Equipment equipment = new Equipment();
        equipment = selectByID(id);
        int originNum = equipment.getEquip_num();
        if(handle_way == EquipmentDao.ADD_EQUIPMENT_NUM){
            equipment.setEquip_num(originNum+num);
        }else{
            equipment.setEquip_num(originNum-num);
        }
        return updateById(equipment,id);

    }

}
